#!/usr/bin/env python
#_*_coding:utf-8_*_
from django.shortcuts import HttpResponse,render
from app01 import models
from app01.forms import forign as ForignForm
def create_user_group(request):
    #数据库的UserGroup_New对象表里插入数据，注释则代表不创建数据
    # models.UserGroup_New.objects.create(caption='CEO')
    # models.UserGroup_New.objects.create(caption='CTO')
    # models.UserGroup_New.objects.create(caption='COO')
    models.UserGroup_New.objects.create(caption='CXO')
    return HttpResponse('ok')
#创建表
def create_user(request):
    obj = ForignForm.UserForm(request.POST)
    if request.method == "POST":
        if obj.is_valid():
            all_data = obj.clean()
            print all_data
            #第1种方式，先获取对象,group_obj表示组的对象
            # group_obj = models.UserGroup_New.objects.get(id=all_data['user_group'])
            # models.UserGroup_New.objects.create(username=all_data['username'],
            #                                     user_group=group_obj)
            # #第2种方式
            # models.User_New.objects.create(username=all_data['username'],
            #                                user_group_id=all_data['user_group'])
            models.User_New.objects.create(**all_data)
            #创建成功后打印下个数
            print models.User_New.objects.all().count()
        else:
            pass
    #下面的usergroup只是在url路径上面的查找用的
    val = request.GET.get('usergroup')
    #注意这里的双下划线__的用法，这里表示查找user_group表下caption字段
    user_list = models.User_New.objects.filter(user_group__caption=val)
    # user_list = models.User_New.objects.all()
    return render(request,'forign/create_user.html',{'obj':obj,'user_list':user_list})
#删除表
def delete_user(request):
    obj = ForignForm.UserForm(request.POST)
    if request.method == "POST":
        if obj.is_valid():
            all_data = obj.clean()
            print all_data


            models.User_New.objects.filter(**all_data).delete()
            #删除成功后打印下个数
            print models.User_New.objects.all().count()
        else:
            pass


    #因为上面用了POST,所以这里用GET，不是POST就是GET
    # val = request.GET.get('username')
    # user_list = models.User_New.objects.filter(username=val)

    #下面的usergroup只是在url路径上面的查找用的
    val = request.GET.get('usergroup')
    #注意这里的双下划线__的用法，这里表示查找user_group表下caption字段
    user_list = models.User_New.objects.filter(user_group__caption=val)
    # user_list = models.User_New.objects.all()
    return render(request,'forign/create_user.html',{'obj':obj,'user_list':user_list})

#修改表
# 改
# models.Tb1.objects.filter(name='seven').update(gender='0')  # 将指定条件的数据更新，均支持 **kwargs
# obj = models.Tb1.objects.get(id=1)
# obj.c1 = '111'
# obj.save()                                                 # 修改单条数据
def upload_user(request):
    obj = ForignForm.UserForm(request.POST)
    if request.method == "POST":
        if obj.is_valid():
            all_data = obj.clean()
            print all_data
            zt = {'username':'testzt','user_group_id':5}

            models.User_New.objects.filter(**all_data).update(**zt)
            # #修改成功后打印下个数
            # print models.User_New.objects.all().count()
        else:
            pass
    #因为上面用了POST,所以这里用GET，不是POST就是GET
    # val = request.GET.get('username')
    # user_list = models.User_New.objects.filter(username=val)

    #下面的usergroup只是在url路径上面的查找用的
    val = request.GET.get('usergroup')
    #注意这里的双下划线__的用法，这里表示查找user_group表下caption字段
    user_list = models.User_New.objects.filter(user_group__caption=val)
    # user_list = models.User_New.objects.all()
    return render(request,'forign/create_user.html',{'obj':obj,'user_list':user_list})